import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TriangleFrame extends JFrame {
    private JLabel label1;
    private JTextField width;
    private JLabel label2;
    private JTextField high;
    private JButton calculate;
    private JTextField areas;
    private Triangle triangle;

    public  TriangleFrame(){
        triangle = new Triangle();

        label1 = new JLabel("width");
        width = new JTextField();
        label2 = new JLabel("high");
        high = new JTextField();
        calculate = new JButton("calculate");
        areas = new JTextField();

        setLayout(new GridLayout(3,2));
        add(label1);
        add(width);
        add(label2);
        add(high);
        add(calculate);
        add(areas);

        //设置监听
        calculate.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                dowriter();
                writetoDb();
            }
        });

        setSize(400,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }
    private void dowriter(){
        //获取三角形的宽度和高度
      double length;
      double height;
      double area;

      length = Double.parseDouble(width.getText());
      height = Double.parseDouble(high.getText());

      area = (length * height) / 2;
      areas.setText("Area" + area);

      triangle.setId(0);
      triangle.setName("苏子豪");
      triangle.setLength(length);
      triangle.setHeight(height);
      triangle.setNumber("202205010347");
      triangle.setArea(area);

      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      String now = LocalDateTime.now().format(formatter);
      triangle.setNow(now);

    }
    private void writetoDb(){

        //测试驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Connect to jdbc....");
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/rgdb1";
            Connection coon = DriverManager.getConnection(url,"root","123456");
            System.out.println("Connection Successful...");
            Statement stmt = coon.createStatement();
            String insert = "insert into triangle values ("+ triangle.getId()+",'"+triangle.getName()+ "',"+triangle.getNumber()+","+triangle.getLength()+","+triangle.getHeight()+","+triangle.getArea()+",'"+triangle.getNow()+"')";
            System.out.println(insert);
            int count = stmt.executeUpdate(insert);
            if(1 == count){
                JOptionPane.showMessageDialog(this,"添加成功");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
